package org.xteam.box2text.oaw;

import org.eclipse.xtend.expression.Resource;
import org.xteam.box2text.BoxToText;
import org.xteam.box2text.box.Box;
import org.xteam.box2text.parser.ast.BoxSpec;
import org.xteam.box2text.parser.ast.Rule;


public class BoxRule {

	private Rule rule;
	private BoxResource resource;

	public BoxRule(BoxResource resource, Rule rule) {
		this.resource = resource;
		this.rule = rule;
	}

	public String evaluate(BoxExecutionContext ctx) {
		BoxEvaluator bv = new BoxEvaluator(ctx);
		rule.getBoxspec().visit(bv);
		Box box = bv.getResult();
		/*try {
			save(Arrays.asList(box), "models/temp.xmi");
		} catch (IOException e) {
		}*/
		BoxToText box2text = new BoxToText();
		return box2text.text(box).toString();
	}
	
	/*private static void save(List<Box> boxes, String filename) throws IOException {
		org.eclipse.emf.ecore.resource.Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());

		ResourceSet rs = new ResourceSetImpl();
		org.eclipse.emf.ecore.resource.Resource res = rs.createResource(URI.createFileURI(filename));
		res.getContents().addAll(boxes);
		
		BoxPackage.eINSTANCE.eResource().setURI(URI.createFileURI("box.ecore"));
		Map<String, Object> options = new HashMap<String, Object>();
		options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
		res.save(options);
	}*/

	public String getTargetType() {
		return rule.getClassName().getValue();
	}

	public Resource getOwner() {
		return resource;
	}

	public BoxSpec getBoxspec() {
		return rule.getBoxspec();
	}

}
